package com.ruoyi.common.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.SysConstant;
import com.ruoyi.common.service.BaseService;
import org.springframework.stereotype.Component;

/**
 * @author seeyon
 */
@Component
public class PageQueryHelper<T> {

    /**
     * 分页查询
     *
     * @param service      service对象
     * @param pageQuery    分页参数
     * @param queryWrapper 查询条件
     * @return Page
     */
    public Page<T> pageInfo(BaseService<T> service, PageQuery<T> pageQuery, QueryWrapper<T> queryWrapper) {
        if (StrUtil.isNotEmpty(pageQuery.getSortName())) {
            if (SysConstant.DEFAULT_SORT_ORDER.equalsIgnoreCase(pageQuery.getSortOrder())) {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(pageQuery.getSortName()));
            } else {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(pageQuery.getSortName()));
            }
        }
        return service.page(new Page<T>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper);
    }
}
